package com.ztom.v2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhangTao
 */
public class Code55Permutation {

    public List<List<Integer>> permute(int[] nums) {
        if (nums == null || nums.length == 0) {
            return new ArrayList<>();
        }
        List<List<Integer>> res = new ArrayList<>();
        dfs(nums, 0, res, new ArrayList<>());
        return res;
    }

    public void dfs(int[] nums, int index, List<List<Integer>> res, List<Integer> path) {
        if (index == nums.length) {
            res.add(new ArrayList<>(path));
            return;
        }
        for (int num : nums) {
            if (path.contains(num)) {
                continue;
            }
            path.add(num);
            dfs(nums, index + 1, res, path);
            path.remove(path.size() - 1);
        }
    }
}
